package demo1.Frames;

import demo1.Dialogs.MyDialog;
import demo1.Layouts.LayoutAdapter;
import demo1.Layouts.MyLayout;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Labels extends JFrame{
    JPanel root = new JPanel();
    Border border = BorderFactory.createRaisedBevelBorder();
    JLabel label1 = new JLabel("1");
    JLabel label2 = new JLabel("2");
    JLabel label3 = new JLabel("3");
    JLabel label4 = new JLabel("4");
    MyDialog dialog = new MyDialog(this);

    public Labels(String title) {
        super(title);
        this.setContentPane(root);
        root.setBorder(BorderFactory.createLineBorder(new Color(255, 152, 152),3));
        root.setLayout(new MyCustomLayout());

        InitLabel(label1);
        InitLabel(label2);
        InitLabel(label3);
        InitLabel(label4);


    }
    public void InitLabel(JLabel label) {
        root.add(label);

        label.setFont(new Font("宋体", Font.BOLD,20));
        label.setForeground(new Color(128, 0, 255, 255));
        label.setOpaque(true);//设置背景不透明
        label.setBackground(new Color(104, 214, 255));
        label.setPreferredSize(new Dimension(250,30));
        label.setHorizontalAlignment(SwingConstants.CENTER);

        label.setBorder(border);

        label4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                dialog.setTitle("error!");
                dialog.setModal(true);
                dialog.setSize(200,100);
                dialog.setVisible(true);
            }
        });
    }





    private class MyCustomLayout extends LayoutAdapter {

        @Override
        public void addLayoutComponent(Component comp, Object constraints) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public void layoutContainer(Container parent) {
            Insets insets = parent.getInsets();
            int width = parent.getWidth();
            int height = parent.getHeight();
            label1.setBounds(insets.left,insets.top,100,100);
            label2.setBounds(width - 100 - insets.right,insets.top,100,100);
            label3.setBounds(insets.left,height - 100 - insets.bottom,100,100);
            label4.setBounds(width - 100 - insets.right,height - 100 - insets.bottom,100,100);
        }
    }
}
